package com.huatai.exchange.bootup.controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import com.huatai.exchange.facade.DataFileExchangeController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class AbstractRestController {


	@Autowired
	protected HttpServletResponse httpResponse;

	protected void setResponseFile(String filename) {
		httpResponse.setDateHeader("Expires", 0L);
		httpResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		httpResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
		httpResponse.setHeader("Pragma", "no-cache");
		httpResponse.setContentType("application/octet-stream");
		httpResponse.setCharacterEncoding("UTF-8");
		String iso_filename = parseGBK(filename);
		httpResponse.setHeader("x-filename", iso_filename);
		httpResponse.setHeader("Content-Disposition", "attachment;filename=\"" + iso_filename + "\"");
	}

	private static String parseGBK(String sIn) {
		try {
			return java.net.URLEncoder.encode(sIn, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}

}
